#include "DistanceMeasurementDrawerFactory.h"
#include "HorizontalDoubleLineDrawer.h"
#include "VerticalDoubleLineDrawer.h"
#include "MeasurementAlgorithmBase.h"

DistanceMeasurementDrawerFactory::DistanceMeasurementDrawerFactory(QObject *parent) 
    : QObject(parent),
      m_scene(nullptr),
      m_horizontalDoubleLineDrawer(nullptr),
      m_verticalDoubleLineDrawer(nullptr)
{}

DistanceMeasurementDrawerFactory::~DistanceMeasurementDrawerFactory()
{
    // 清理绘制器实例
    delete m_horizontalDoubleLineDrawer;
    delete m_verticalDoubleLineDrawer;
}

void DistanceMeasurementDrawerFactory::setScene(QGraphicsScene *scene)
{
    m_scene = scene;
    
    // 更新所有绘制器的场景
    if (m_horizontalDoubleLineDrawer) {
        m_horizontalDoubleLineDrawer->setScene(scene);
    }
    if (m_verticalDoubleLineDrawer) {
        m_verticalDoubleLineDrawer->setScene(scene);
    }
}

QObject *DistanceMeasurementDrawerFactory::getDrawer(MeasurementMode mode)
{
    switch (mode) {
    case MeasurementMode::VerticalDoubleLine:
        if (!m_horizontalDoubleLineDrawer) {
            m_horizontalDoubleLineDrawer = new HorizontalDoubleLineDrawer(this);
            if (m_scene) {
                m_horizontalDoubleLineDrawer->setScene(m_scene);
            }
            // 连接绘制器信号到工厂信号
            connect(m_horizontalDoubleLineDrawer, &HorizontalDoubleLineDrawer::lineChanged, 
                    this, &DistanceMeasurementDrawerFactory::horizontalDoubleLineChanged);
        }
        return m_horizontalDoubleLineDrawer;
        
    case MeasurementMode::HorizontalDoubleLine:
        if (!m_verticalDoubleLineDrawer) {
            m_verticalDoubleLineDrawer = new VerticalDoubleLineDrawer(this);
            if (m_scene) {
                m_verticalDoubleLineDrawer->setScene(m_scene);
            }
            // 连接绘制器信号到工厂信号
            connect(m_verticalDoubleLineDrawer, &VerticalDoubleLineDrawer::lineChanged, 
                    this, &DistanceMeasurementDrawerFactory::verticalDoubleLineChanged);
        }
        return m_verticalDoubleLineDrawer;
        
    default:
        // 不支持的测量模式
        return nullptr;
    }
}

void DistanceMeasurementDrawerFactory::setParameters(const QVariantMap &params)
{
    // 设置所有绘制器的参数
    if (m_horizontalDoubleLineDrawer) {
        m_horizontalDoubleLineDrawer->setParameters(params);
    }
    if (m_verticalDoubleLineDrawer) {
        m_verticalDoubleLineDrawer->setParameters(params);
    }
}

void DistanceMeasurementDrawerFactory::clearAll()
{
    // 清除所有绘制器的内容
    if (m_horizontalDoubleLineDrawer) {
        m_horizontalDoubleLineDrawer->clearAll();
    }
    if (m_verticalDoubleLineDrawer) {
        m_verticalDoubleLineDrawer->clearAll();
    }
}

void DistanceMeasurementDrawerFactory::updateScale(double scale)
{
    // 更新所有绘制器的缩放比例
    if (m_horizontalDoubleLineDrawer) {
        m_horizontalDoubleLineDrawer->setScale(scale);
    }
    if (m_verticalDoubleLineDrawer) {
        m_verticalDoubleLineDrawer->setScale(scale);
    }
}

void DistanceMeasurementDrawerFactory::initializeMeasurementTool(MeasurementMode mode, int imageWidth, 
                                                              int imageHeight, double scale, 
                                                              const QPointF &offset)
{
    // 首先清除所有绘制内容
    clearAll();
    
    // 根据模式初始化对应的测量工具
    switch (mode) {
    case MeasurementMode::VerticalDoubleLine:
        if (!m_horizontalDoubleLineDrawer) {
            m_horizontalDoubleLineDrawer = new HorizontalDoubleLineDrawer(this);
            if (m_scene) {
                m_horizontalDoubleLineDrawer->setScene(m_scene);
            }
            connect(m_horizontalDoubleLineDrawer, &HorizontalDoubleLineDrawer::lineChanged,
                    this, &DistanceMeasurementDrawerFactory::horizontalDoubleLineChanged);
        }
        m_horizontalDoubleLineDrawer->initializeLine(imageWidth, imageHeight, scale, offset);

        break;
    case MeasurementMode::HorizontalDoubleLine:
        if (!m_verticalDoubleLineDrawer) {
            m_verticalDoubleLineDrawer = new VerticalDoubleLineDrawer(this);
            if (m_scene) {
                m_verticalDoubleLineDrawer->setScene(m_scene);
            }
            connect(m_verticalDoubleLineDrawer, &VerticalDoubleLineDrawer::lineChanged,
                    this, &DistanceMeasurementDrawerFactory::verticalDoubleLineChanged);
        }
        m_verticalDoubleLineDrawer->initializeLine(imageWidth, imageHeight, scale, offset);
        break;
    default:
        // 不支持的测量模式
        break;
    }
}
